/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package geracaopdf;

/**
 *
 * @author Igor
 */
import java.util.ArrayList;
import utils.Cliente;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Transacoes;

public class TransacoesListaPDF {

    private ArrayList<Transacoes> transacoesDaLista;
    private static String file;
    private Cliente clienteEscolhido;
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
    private static Font bigBold = new Font(Font.FontFamily.TIMES_ROMAN, 14,
            Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
            Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);
    private static Font smallestBold = new Font(Font.FontFamily.TIMES_ROMAN, 6,
            Font.BOLD);
    private static Font capsNoBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.NORMAL);
    private static Font smallNormal = new Font(Font.FontFamily.TIMES_ROMAN, 10,
            Font.NORMAL);

    public TransacoesListaPDF(ArrayList<Transacoes> transacoesDaLista) {
        this.transacoesDaLista = transacoesDaLista;
        Document document = new Document();
        file = "C:/Arquivos PDF/Compras/"+"Compra " + new Date().getHours() + " e " + new Date().getMinutes() + ".pdf";
        try {
            try {
                PdfWriter.getInstance(document, new FileOutputStream(file));
                document.open();
                addMetaData(document);
                addTitlePage(document);
                document.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ClienteIndividualPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(ClienteIndividualPDF.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void addMetaData(Document document) {
        document.addTitle("Lista de transações ");
        document.addSubject("Notas de venda");
        document.addKeywords("Java, PDF, Transações");
        document.addAuthor("Frente de Caixa");
        document.addCreator("Frente de Caixa");
    }

    private void addTitlePage(Document document)
            throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 2);
        // Lets write a big header
        preface.add(new Paragraph("Dados referentes à lista de clientes gerada", catFont));

        addEmptyLine(preface, 2);
        createTable(preface);
        addEmptyLine(preface, 16);
        // Will create: Report generated by: _name, _date
        Paragraph textoFinal = new Paragraph("Este relatório foi gerado pelo Frente de Caixa em:" + new Date(), smallestBold);
        textoFinal.setAlignment(Element.ALIGN_CENTER);
        preface.add(textoFinal);

        document.add(preface);
        document.newPage();

    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private void createTable(Paragraph subCatPart)
            throws BadElementException {
        int[] relativeWidths = {50, 150, 80, 120, 80};

        for (int i = 0; i < transacoesDaLista.size(); i++) {

            PdfPTable table = new PdfPTable(5);
            try {
                table.setWidths(relativeWidths);

            } catch (DocumentException ex) {
                Logger.getLogger(ClienteListaPDF.class.getName()).log(Level.SEVERE, null, ex);
            }

            // t.setBorderColor(BaseColor.GRAY);
            // t.setPadding(4);
            // t.setSpacing(4);
            // t.setBorderWidth(1);

            Phrase frase = new Phrase("ID", smallBold);
            PdfPCell c1 = new PdfPCell(frase);
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            frase = new Phrase("Cliente", smallBold);
            c1 = new PdfPCell(frase);
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            frase = new Phrase("CPF/CNPJ", smallBold);
            c1 = new PdfPCell(frase);
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            frase = new Phrase("Produto", smallBold);
            c1 = new PdfPCell(frase);
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            frase = new Phrase("Código", smallBold);
            c1 = new PdfPCell(frase);
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            table.setHeaderRows(1);

            Transacoes transacaoDaVez = transacoesDaLista.get(i);
            Phrase fraseTabela = new Phrase(String.valueOf(transacaoDaVez.getIdDaTransacao()).toUpperCase(), smallNormal);
            table.addCell(fraseTabela);

            fraseTabela = new Phrase(transacaoDaVez.getNomeDoCliente().toUpperCase(), smallNormal);
            table.addCell(fraseTabela);

            fraseTabela = new Phrase(transacaoDaVez.getCpf_cnpjCliente().toUpperCase(), smallNormal);
            table.addCell(fraseTabela);

            fraseTabela = new Phrase(transacaoDaVez.getNomeDoProduto().toUpperCase(), smallNormal);
            table.addCell(fraseTabela);

            fraseTabela = new Phrase(transacaoDaVez.getCodigoDoProduto().toUpperCase(), smallNormal);
            table.addCell(fraseTabela);

            // Lets write a big header
            subCatPart.add(new Paragraph("________________________________________________________________________________", smallBold));
            subCatPart.add(new Paragraph("Quantidade do produto: " + transacaoDaVez.getQuantidadeVendidade(), smallBold));
            subCatPart.add(new Paragraph("Preço por unidade (R$): " + transacaoDaVez.getPrecoPorUnidade(), smallBold));
            subCatPart.add(new Paragraph("Valor da transação (R$): " + transacaoDaVez.getValorTotalDaTransacao(), smallBold));
            subCatPart.add(new Paragraph("Desconto dado (R$): " + transacaoDaVez.getDescontoDado(), smallBold));
            subCatPart.add(new Paragraph("Valor final (R$): " + (transacaoDaVez.getValorTotalDaTransacao() - transacaoDaVez.getDescontoDado()), smallBold));
            subCatPart.add(new Paragraph("Data (ano-mês-dia): " + (transacaoDaVez.getData().toString()), smallBold));
            addEmptyLine(subCatPart, 2);
            subCatPart.add(table);
        }


    }
}
